package com.d3.pomodoro.utils

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import com.d3.pomodoro.R
import com.d3.pomodoro.config.HawkConfig
import com.orhanobut.hawk.Hawk
import com.orhanobut.logger.Logger


@Suppress("DEPRECATION")
class ThemeController(private var activity: Activity) {

    var currModel = Hawk.get(HawkConfig.THEME_KEY, ThemeModel.THEME_PINK)

    enum class ThemeModel {
        THEME_PINK,
        THEME_BLUE,
        THEME_DARK_BLUE,
        THEME_PINK_PINK,
        THEME_PURPLE,
        THEME_ORANGE,
        THEME_YELLOW,
        THEME_GREEN,
        THEME_GREEN_DARK,
        THEME_GRAY_DARK,
        THEME_GREEN_LIGHT
    }

    lateinit var mainColor: TypedValue
    lateinit var secondColor: TypedValue
    lateinit var thirdColor: TypedValue
    lateinit var fourthColor: TypedValue

    val mainColorViewList = ArrayList<View>()
    val secondColorViewList = ArrayList<View>()
    val thirdColorViewList = ArrayList<View>()
    val fourthColorViewList = ArrayList<View>()

    init {
        mainColorViewList.clear()
        secondColorViewList.clear()
        thirdColorViewList.clear()
        fourthColorViewList.clear()

    }

    fun initTheme() {
        changeTheme(currModel, true)
    }

    fun changeTheme(themeModel: ThemeModel, isInit: Boolean) {
        Logger.e("changeTheme themeModel is " + themeModel)
        if (currModel != themeModel || isInit) {
            currModel = themeModel
            Hawk.put(HawkConfig.THEME_KEY, currModel)
            showAnimation()
            toggleThemeSetting()
            refreshThemeUI()
        } else {
            Logger.e("不能切换成当前主题")

        }
    }

    private fun showAnimation() {
        val decorView = activity.window.decorView
        val cacheBitmap = getCacheBitmapFromView(decorView)
        if (decorView is ViewGroup && cacheBitmap != null) {
            val view = View(activity)
            view.setBackgroundDrawable(BitmapDrawable(activity.resources, cacheBitmap))
            val layoutParam = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            decorView.addView(view, layoutParam)
            val objectAnimator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f)
            objectAnimator.duration = 300
            objectAnimator.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    decorView.removeView(view)
                }
            })
            objectAnimator.start()
        }
    }

    private fun getCacheBitmapFromView(view: View): Bitmap? {
        val drawingCacheEnabled = true
        view.isDrawingCacheEnabled = drawingCacheEnabled
        view.buildDrawingCache(drawingCacheEnabled)
        val drawingCache = view.drawingCache
        val bitmap: Bitmap?
        if (drawingCache != null) {
            bitmap = Bitmap.createBitmap(drawingCache)
            view.isDrawingCacheEnabled = false
        } else {
            bitmap = null
        }
        return bitmap
    }

    private fun toggleThemeSetting() {
        val currTheme = when (currModel) {
            ThemeModel.THEME_BLUE -> R.style.AppBlueTheme
            ThemeModel.THEME_DARK_BLUE -> R.style.AppDarkBlueTheme
            ThemeModel.THEME_PINK_PINK -> R.style.AppPinkPinkTheme
            ThemeModel.THEME_PURPLE -> R.style.AppPurpleTheme
            ThemeModel.THEME_ORANGE -> R.style.AppOrangeTheme
            ThemeModel.THEME_YELLOW -> R.style.AppYellowTheme
            ThemeModel.THEME_GREEN -> R.style.AppGreenTheme
            ThemeModel.THEME_GREEN_DARK -> R.style.AppGreenDarkTheme
            ThemeModel.THEME_GRAY_DARK -> R.style.AppGrayDarkTheme
            ThemeModel.THEME_GREEN_LIGHT -> R.style.AppGreenLightTheme
            else -> R.style.AppPinkTheme
        }
        activity.setTheme(currTheme)
    }

    private fun refreshThemeUI() {
        mainColor = TypedValue()
        secondColor = TypedValue()
        thirdColor = TypedValue()
        fourthColor = TypedValue()
        val theme = activity.theme
        theme.resolveAttribute(R.attr.MainColor, mainColor, true)
        theme.resolveAttribute(R.attr.SecondColor, secondColor, true)
        theme.resolveAttribute(R.attr.ThirdColor, thirdColor, true)
        theme.resolveAttribute(R.attr.FourthColor, fourthColor, true)
        refreshStatusBar()
        refreshOtherView()

    }

    private fun refreshOtherView() {
        for (view in mainColorViewList) {
            view.setBackgroundColor(activity.resources.getColor(mainColor.resourceId))
        }
        for (view in secondColorViewList) {
            view.setBackgroundColor(activity.resources.getColor(secondColor.resourceId))
        }
        for (view in thirdColorViewList) {
            view.setBackgroundColor(activity.resources.getColor(thirdColor.resourceId))
        }
        for (view in fourthColorViewList) {
            view.setBackgroundColor(activity.resources.getColor(fourthColor.resourceId))
        }

    }

    /**
     * 刷新 StatusBar
     */
    private fun refreshStatusBar() {
        if (Build.VERSION.SDK_INT >= 21) {
            val typedValue = TypedValue()
            val theme = activity.theme
            theme.resolveAttribute(R.attr.MainColor, typedValue, true)
            activity.window.statusBarColor = activity.resources.getColor(typedValue.resourceId)
        }
    }


}